/**
 * @Author: SevDaisy十七散人 
 * @Date: 2020-07-08 21:18:27
 */
package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import zyy.util.BusinessException;
import zyy.util.Bus;

public class FrmAddrReg extends JDialog implements ActionListener {
  // 本来想用 JcomboBox 做的，但是好麻烦（很难做好省市区的级联绑定），所以，算了。嘿嘿嘿
  // String[] listProvince = new String[] { "浙江", "北京" };
  private static final long serialVersionUID = 4170246632337548293L;
  private JLabel addrProvinceLabel = new JLabel("省："); // 省
  private JLabel addrCityLabel = new JLabel("市："); // 市
  private JLabel addrBlockLabel = new JLabel("区："); // 区
  private JLabel addrDatilLabel = new JLabel("精确："); // 精确
  private JLabel targetNameLabel = new JLabel("姓名："); // 收件人姓名
  private JLabel TelphoneLabel = new JLabel("电话："); // 收件人电话
  private JTextField addrProvinceEdt = new JTextField(20);
  private JTextField addrCityEdt = new JTextField(20);
  private JTextField addrBlockEdt = new JTextField(20);
  private JTextField addrDatilEdt = new JTextField(20);
  private JTextField targetNameEdt = new JTextField(20);
  private JTextField TelphoneEdt = new JTextField(20);

  private JButton commit = new JButton("确认");
  private JButton exit = new JButton("取消");

  private JPanel text = new JPanel();
  private JPanel btns = new JPanel();

  private String frmType;

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.commit) {
      String P = this.addrProvinceEdt.getText();
      String C = this.addrCityEdt.getText();
      String B = this.addrBlockEdt.getText();
      String D = this.addrDatilEdt.getText();
      String N = this.targetNameEdt.getText();
      String T = this.TelphoneEdt.getText();
      System.out.println("地址注册 提交" + String.format("地址：%s %s %s %s %s:%s", P, C, B, D, N, T));
      try {
        switch (this.frmType) {
          case "用户":
            Bus.addrManager.add("用户", Bus.currentUser.getUserID(), P, C, B, D, N, T);
            Bus.fuUserAddress.reload();
            break;
          case "商家":
            Bus.addrManager.add("商家", 0, P, C, B, D, N, T);
            Bus.fNewMerchant.reload();
            break;
          default:
            break;
        }
        JOptionPane.showMessageDialog(null, String.format("地址：%s %s %s %s %s:%s", P, C, B, D, N, T), "注册成功",
            JOptionPane.INFORMATION_MESSAGE);
        this.setVisible(false);
      } catch (BusinessException ex) {
        ex.printStackTrace();
      }
    } else if (e.getSource() == this.exit) {
      this.setVisible(false);
    }
  }

  public FrmAddrReg(String type) {
    this.frmType = type;
    // JDialog 基础设置
    {
      this.setTitle("外卖助手 " + type + " 地址注册");
      this.setSize(285, 380);
      this.setLocationRelativeTo(null);
      this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
      this.setVisible(true);
    }
    // 布局
    {
      // ContentPane 一级设置
      {
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(text, BorderLayout.CENTER);
        this.getContentPane().add(btns, BorderLayout.SOUTH);
      }
      // text 设置
      {
        this.targetNameLabel.setText(type + "名字：");
        this.TelphoneLabel.setText(type + "电话：");
        text.setLayout(new FlowLayout(FlowLayout.LEFT));
        text.add(addrProvinceLabel);
        text.add(addrProvinceEdt);
        text.add(addrCityLabel);
        text.add(addrCityEdt);
        text.add(addrBlockLabel);
        text.add(addrBlockEdt);
        text.add(addrDatilLabel);
        text.add(addrDatilEdt);
        text.add(targetNameLabel);
        text.add(targetNameEdt);
        text.add(TelphoneLabel);
        text.add(TelphoneEdt);
      }
      // btns 设置
      {
        btns.setLayout(new FlowLayout(FlowLayout.RIGHT));
        btns.add(commit);
        btns.add(exit);
        this.commit.addActionListener(this);
        this.exit.addActionListener(this);
      }
    }
  }
}